package com.fdc_project.controller;

import com.fdc_project.service.FileSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@Controller("/fileService")
public class FileSystemController {
    @Autowired
    FileSystemService fileSystemService;

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public Boolean upload(@RequestParam("files") List<MultipartFile> files,String pro_name,HttpServletRequest request) {
        if (files.isEmpty()) {
            return false;
        }
        for (MultipartFile file : files) {                                           //添加多个文件
            String fileName = file.getOriginalFilename();
//            int size = (int) file.getSize();
//            System.out.println(fileName + "-->" + size);
            if (file.isEmpty()) {
                return false;
            }
            else
            {
                try {
                    File dest = multipartFileToFile(file);                          //添加文件名后缀
                    fileSystemService.uploadFile("/"+pro_name+"/" + fileName, dest);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

    @PostMapping("/download")
    public String download(HttpServletResponse httpServletResponse,String pro_name) throws Exception{
        fileSystemService.packge(pro_name);                                            //给项目文件夹打包
        String downurl = "/root/"+pro_name+".zip";
        File file = fileSystemService.downloadFile(downurl);
        String filename  = file.getName();
        if (file.exists()) { //判断文件父目录是否存在
            httpServletResponse.setContentType("application/zip;charset=UTF-8");
            httpServletResponse.setCharacterEncoding("UTF-8");
            // response.setContentType("application/force-download");
            httpServletResponse.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = httpServletResponse.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download---" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @PostMapping("/creatDirectory")
    @ResponseBody
    public boolean creatDirectory(String pro_name) throws Exception{
        fileSystemService.createDirectory(pro_name);
        return true;
    }

    @PostMapping("/packge")
    @ResponseBody
    public boolean packge(String pro_name) throws Exception{
        fileSystemService.packge(pro_name);
        return true;
    }

    public static File multipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        System.out.println(fileName);
        // 获取文件后缀
//        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码
        try {
            File file = File.createTempFile(fileName,"");
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
